package com.kaka.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kaka.pojo.Permission;
import com.kaka.pojo.Role;
import com.kaka.pojo.Role_Pid;
import com.kaka.service.PermissionService;
import com.kaka.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private PermissionService permissionService;

    @GetMapping("/findAll")
    public String findAll(@RequestParam(value = "page", defaultValue = "1", required = false) Integer page,
                          @RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit,
                          HttpServletRequest request) {
        PageHelper.startPage(page, limit);
        List<Role> roleList = roleService.findAll();
        PageInfo<Role> pageInfo = new PageInfo<>(roleList);
        request.setAttribute("pageInfo", pageInfo);
        return "/role/role-list.jsp";
    }

    @GetMapping("/show")
    public String show(@RequestParam("id") Integer id, HttpServletRequest request) {
        Role role = roleService.findById(id);
        request.setAttribute("role", role);
        return "/role/role-show.jsp";
    }

    @GetMapping("/findById")
    public String findById(@RequestParam("id") Integer id, HttpServletRequest request) {
        Role role = roleService.findById(id);
        request.setAttribute("role", role);
        return "/role/role-edit.jsp";
    }

    @PostMapping("/edit")
    public String edit(Role role) {
        int i = roleService.editRole(role);
        return "redirect:findAll";
    }

    @GetMapping("/permissionAdd")
    public String permissionAdd(@RequestParam("id") int rid, HttpServletRequest request) {
        //先查询当前角色拥有的权限
        List<Integer> permissionIdList = roleService.findPermissionByRid(rid);
        //将当前角色拥有的权限发送
        request.setAttribute("permissionIdList", permissionIdList);
        //查询所有权限数据
        List<Permission> permissionList = permissionService.findAll();
        //将所有权限发送
        request.setAttribute("permissionList", permissionList);

        request.setAttribute("roleId", rid);

        return "/role/role-edit-permission.jsp";
    }

    @PostMapping("/permissionAdd")
    @ResponseBody
    public String permissionAdd(@RequestBody Role_Pid rp) {
        roleService.editPermission(rp);
        return "/role/findAll";
    }

    @PostMapping("/add")
    public String addRole(Role role) {
        int i = roleService.addRole(role);
        return "redirect:findAll";
    }

    @GetMapping("/delete")
    public String deleteRole(@RequestParam("id") int rid) {
        int i = roleService.deleteRole(rid);
        return "redirect:findAll";
    }

    @RequestMapping("/deleteIds")
    public String deleteRoles(@RequestParam("ids") List<Integer> ids) {
        /*System.out.println(usersRolePermission);
        System.out.println(usersRolePermission.getIds());*/
/*
        List<Integer> ids = usersRolePermission.getIds();*/
        int i = roleService.deleteRoles(ids);
        return "redirect:findAll";
    }
}